﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using AGRTraining.za.co.agrtraining.model;
using AGRTraining.za.co.agrtraining.repository.Impl;
namespace AGRTraining.za.co.agrtraining.services.impl
{
    public class StudentServiceImpl:IStudentService
    {
        private StudentDAO studDao= new StudentDAO();
        private CourseDAO courseDao = new CourseDAO();
        private AGRModelContainer agrDb= new AGRModelContainer();
        
        public Student getStudentDetails(string studCode)
        {

            Student stud = new Student();
            stud=studDao.findById(studCode);
            return stud;
        }

        public void addNewStudent(Hashtable stud)
        {
            Student student = new Student();
            student.StudentFirstName =(string) stud["studFName"];
            student.StudentLastName = (string)stud["studLName"];
            student.StudentDisability = (string)stud["studDisable"];
            student.StudentEquity = (string)stud["studEquity"];
            student.StudentFirstLanguage = (string)stud["studFLanguage"];
            student.StudentGender = (string)stud["studGender"];
            student.StudentIDNumber = (string)stud["studIdNum"];
            student.StudentNationality = (string)stud["studNationality"];
            student.StudentPhysicalAddress = (string)stud["studPhysAddress"];
            student.StudentPostalAddress = (string)stud["studPostAddress"];
            student.StudentProvince = (string)stud["studProvince"];
            student.StudentResidentialStatus = (string)stud["studResStatus"];
            student.StudentSecondLanguage = (string)stud["studSLanguage"];
            student.StudentSocioStatus = (string)stud["studSocioStatus"];
            studDao.persist(student);
            studDao.commit();
        }

        public void updateStudentDetails(Student stud) {
            studDao.merge(stud);
            studDao.commit();
        }

        public void addCourse(string studCode, string courseCode)
        {
            //this or database procedure to be exeuted here
            //var courseId = from cId in agrDb.Courses
            //               where cId.CourseCode == courseCode
            //               select cId.Id;
            //var studId = from sId in agrDb.Students
            //             where sId.StudentCode == studCode
            //             select sId.Id;
            FactStudentCourse studCourses = new FactStudentCourse();
            studCourses.CourseCode = courseCode;
            studCourses.StudentCode = studCode;
            studCourses.DateRegistered = DateTime.Today;
            agrDb.FactStudentCourses.AddObject(studCourses);
        }
        public IEnumerable<Subject> getStudentSubjects(string studCode) {
            //works perfectly
            var subs = from stud in agrDb.Students
                       from factStud in agrDb.FactStudentCourses
                       from course in agrDb.Courses
                       from courseSub in agrDb.FactCourseSubjectTrainers
                       from sub in agrDb.Subjects
                       where stud.StudentCode == studCode
                       where stud.StudentCode == factStud.StudentCode
                       where factStud.CourseCode == course.CourseCode
                       where course.CourseCode == courseSub.CourseCode
                       where courseSub.SubjectCode == sub.SubjectCode
                       select sub;
            IEnumerable<Subject> subjects = new List<Subject>();
            subjects = subs;
            return subjects;
        }
        public IEnumerable<Student> viewAllStudents()
        {
            IEnumerable<Student> students = new List<Student>();
            students = studDao.findAll();
            return students;

        }
        public IEnumerable<Project> viewAllStudentPlacementHistory(string studCode)
        {
            var projs = from stud in agrDb.Students
                        from factPlace in agrDb.FactPlacementHistories
                        from place in agrDb.Placements1
                        from proj in agrDb.Projects
                        where stud.StudentCode == studCode
                        where stud.StudentCode == factPlace.StudentCode
                        where factPlace.PlacementCode == place.PlacementCode
                        where place.ProjectCode == proj.ProjectCode
                        select proj;
            IEnumerable<Project> projects = new List<Project>();
            projects = projs;
            return projects;
        }
        public IEnumerable<AttendanceCertificate> viewStudentAttendanceCertificate(string studCode)
        {
            var certs = from stud in agrDb.Students
                        from factStudCert in agrDb.FactStudentCertificates
                        from cert in agrDb.AttendanceCertificates
                        where stud.StudentCode == studCode
                        where stud.StudentCode == factStudCert.StudentCode
                        where factStudCert.CertificateCode == cert.CertificateCode
                        select cert;
            IEnumerable<AttendanceCertificate> certificates = new List<AttendanceCertificate>();
            certificates = certs;
            return certificates;

        }
       
        public IEnumerable<Student> viewAllStudentsInSpecifiedYear(int year)
        {
            var studs = from factStud in agrDb.FactStudentCourses
                        from stud in agrDb.Students
                        where factStud.DateRegistered.Value.Year == year
                        where factStud.StudentCode == stud.StudentCode
                        select stud;
            return studs;
        }
        public IEnumerable<Subject> viewIncompletedSubjects(string studCode)
        {
            //MUST BE TESTED *IMPORTANT*
            var subs = from stud in agrDb.Students
                       from sub in agrDb.Subjects
                       from factStudCrs in agrDb.FactStudentCourses
                       from studCrsProg in agrDb.StudentCourseProgresses
                       from studSubProg in agrDb.StudentSubjectProgresses
                       where stud.StudentCode == studCode
                       where stud.StudentCode == factStudCrs.StudentCode
                       where factStudCrs.CourseCode == studCrsProg.CourseCode
                       where factStudCrs.StudentCode == studCrsProg.StudentCode
                       where studCrsProg.StudentCode == studSubProg.StudentCode
                       where studCrsProg.CourseCode == studSubProg.SubjectCode
                       where studSubProg.SubjectCode == sub.SubjectCode
                       where studSubProg.Completed==0
                       select sub;
            return subs;
        }
        public IEnumerable<Subject> viewCompletedSubjects(string studCode)
        {
            //MUST BE TESTED *IMPORTANT*
            var subs = from stud in agrDb.Students
                       from sub in agrDb.Subjects
                       from factStudCrs in agrDb.FactStudentCourses
                       from studCrsProg in agrDb.StudentCourseProgresses
                       from studSubProg in agrDb.StudentSubjectProgresses
                       where stud.StudentCode == studCode
                       where stud.StudentCode == factStudCrs.StudentCode
                       where factStudCrs.CourseCode == studCrsProg.CourseCode
                       where factStudCrs.StudentCode == studCrsProg.StudentCode
                       where studCrsProg.StudentCode == studSubProg.StudentCode
                       where studCrsProg.CourseCode == studSubProg.SubjectCode
                       where studSubProg.SubjectCode == sub.SubjectCode
                       where studSubProg.Completed == 1
                       select sub;
            return subs;
        }
    }
}